package com.manjusaka.create6.clone1;

/**
 * Created by Administrator on 2017/7/8.
 */
public class ObjectClone implements Cloneable {
    private int id;
    private String attr;

    private ObjectClone(Builder builder) {
        this.id = builder.id;
        this.attr = builder.attr;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setAttr(String attr) {
        this.attr = attr;
    }


    public static final class Builder {
        private int id;
        private String attr;

        public Builder setId(int id) {
            this.id = id;
            return this;
        }

        public Builder setAttr(String attr) {
            this.attr = attr;
            return this;
        }

        public ObjectClone build() {
            return new ObjectClone(this);
        }
    }


    public ObjectClone clone1() {
        return new Builder().setAttr(attr).setId(id).build();
    }

    public ObjectClone clone2() {
        Object clone = null;
        try {
            clone = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return (ObjectClone) clone;
    }
}

class Test{
    public static void main(String[] args){
//        ObjectClone objectClone = new Builder().setAttr("555").setId(1).build();
//        ObjectClone objectClone1 = objectClone.clone1();
//        ObjectClone objectClone2 = objectClone.clone2();
    }
}
